<?php

class PilgrimController extends CController
{
	private $_tourTypeId = 2;
	
	public function actionIndex()
	{
		$this->setPageTitle('Паломнические туры');
		
		$this->render('index');
	}
	
	public function actionGetdata()
	{
		$start = (intval($_GET['start']) >= 0) ? intval($_GET['start']) : 0;
		$limit = (intval($_GET['limit']) >= 0) ? intval($_GET['limit']) : 20;
		$sort  = in_array(strtolower($_GET['sort']), array('id', 'title', 'price')) ? strtolower($_GET['sort']) : 'title'; 
		$dir   = in_array(strtolower($_GET['dir']), array('asc', 'desc')) ? strtolower($_GET['dir']) : 'asc';
		$callback = $_GET['callback'];

		$records = Tour::model()
					->criteria($sort, $dir, $start, $limit)
					->findAll('tourTypeId=:tourTypeId', array(':tourTypeId' => $this->_tourTypeId));

		$count = Tour::model()
					->count('tourTypeId=:tourTypeId', array(':tourTypeId' => $this->_tourTypeId));
		
		$this->renderPartial('json', array(
			'data'   => $records,
			'count'  => $count,
			'callbackFunction' => $callback
		), false, true);
	}
	
	public function actionCreate()
	{
		$this->setPageTitle('Паломнические туры');
		
		$this->render('create');
	}
	
	public function actionCreatehandler()
	{
		$tour = new Tour();
		$tour->attributes = array(
			'tourTypeId' => $this->_tourTypeId,
			'title'  => $_POST['Tour']['title'],
			'price'  => $_POST['Tour']['price'],
			'price2' => $_POST['Tour']['priceShek'],
			'route'  => $_POST['Tour']['route']
		);
			
		if ($tour->save()) {
			$tour->saveAttractions($tour->id, (array_key_exists('Attraction', $_POST) ? $_POST['Attraction'] : array()));
				
			FlashMessage::saveInfo(array(
				'title'  => 'Инфо',
				'text'   => 'Паломнический тур сохранен'
			));
		}
		
		$this->redirect('/admin/pilgrim/');
	}
	
	public function actionEdit()
	{
		$this->setPageTitle('Паломнические туры');
		
		$id = intval($_GET['id']);
		
		if ($id > 0) {
			$tour = Tour::model()->with(
				array('attractions')
			)->findByPk($id);
			
			$this->render('edit', array(
				'tour'  => $tour,
				'attractions'=> $tour->getRelated('attractions')
			));
		}
	}
	
	public function actionEdithandler()
	{
		$tourId = intval($_GET['id']);
		
		$tour = new Tour();
		
		if (isset($_POST['Tour'])) {
			if (Tour::model()->findByPk($tourId)) {
				
				$data = array(
					'tourTypeId' => $this->_tourTypeId,
					'title'  => $_POST['Tour']['title'],
					'price'  => $_POST['Tour']['price'],
					'price2' => $_POST['Tour']['priceShek'],
					'route'  => $_POST['Tour']['route']
				);
					
				Tour::model()->updateByPk($tourId, $data);
				$tour->saveAttractions($tourId, (array_key_exists('Attraction', $_POST) ? $_POST['Attraction'] : array()));
					
				FlashMessage::saveInfo(array(
					'title'  => 'Инфо',
					'text'   => 'Паломнический тур сохранен'
				));

            }
		}
		
		$this->redirect('/admin/pilgrim/');
	}
	
	public function actionRemove()
	{
		$id = intval($_GET['id']);

		if ($id > 0) {
			Tour::model()->deleteByPk($id);
		}
		
		die();
	}
}